/*
 * Copyright 2011 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.bize.knexions.android.knexion.twitter;


import java.util.List;

import nl.bize.knexions.android.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ListView;

/**
 * Activity used for registering new Twitter Acounts
 * 
 * @author Joost A. Bloemsma
 */
public class RegisterActivity extends Activity {
	private TwitterConnection conn;
	final RegisterActivity _me = this;
	private EditText etName;
	private ListView accList;
	
	private OnClickListener pinBtnListener = new OnClickListener() {
		public void onClick(View v) {
			_me.onGetPin();
		}
	};
	
	private OnClickListener setPinBtnListener = new OnClickListener() {
		public void onClick(View v) {
			_me.onSetPin();
		}
	};
	
	/** 
	 * Called when the activity is first created. 
     * {@inheritDoc}
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.registertwitter);
        findViewById(R.id.btnPin).setOnClickListener(pinBtnListener);
        findViewById(R.id.btnSetPin).setOnClickListener(setPinBtnListener);
        etName = (EditText)findViewById(R.id.etName);
        accList = (ListView)findViewById(R.id.lstAccounts);
        conn = new TwitterConnection();
    }
    
    /**
	 * {@inheritDoc}
	 */
	@Override
	protected void onResume() {
		super.onResume();
		List<String> names = TwitterAccountStore.getNames();
		
	}

	/**
     * 
     */
    void onGetPin() {
    	String url = conn.getTokenUrl();
    	Intent i = new Intent(Intent.ACTION_VIEW);
    	i.setData(Uri.parse(url));
    	startActivity(i);
    }
    
    /**
     * 
     */
    void onSetPin() {
    	EditText txt = (EditText)findViewById(R.id.ulrTxt);
		String pin = txt.getText().toString();
		String name = etName.getText().toString();
		if (!conn.checkPin(name, pin)) {
			txt.setText("INVALID!");
		}
    }
}

